<template>
  <div>
    <div id="app" class="wrapper wrapper-content">
      <div class="row">
        <div>
          <div class="panel panel-primary">
              <div class="panel-body" style="padding: 0px 120px; box-sizing: border-box;">
                  <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :disabled="formReadOnly">
                      <div  style="text-align:center">
                          <h3 style="font-weight: bold;color: black;font-size:24px">公司福利申请单</h3>
                      </div>
                      <table style="margin:0 auto;width:80%;margin-top:20px;border-collapse: collapse;margin-bottom:20px">
                          <tr style="line-height: 3.5;">
                              <td colspan="4"
                                  style="font-weight: bold;text-align: left;padding-left: 32px;position: relative;font-size: 14px">
                                  <img src="../../../../../assets/imgs/processManagement/work.png" alt=""
                                      style="position: absolute;width: 20px;height: 20px;left: 0px;top: 14px;margin-left: 8px">
                                  基本信息
                              </td>
                          </tr>
                          <tr class="iconHidden">
                              <td class="width-15">申请人</td>
                              <td class="width-30" >
                                  <el-form-item  prop="applyuser.id">
                                        <user-select :limit='1' :value="inputForm.applyuser.id" @getValue='(value) => {inputForm.applyuser.id=value}' disabled></user-select>
                                  </el-form-item> 
                              </td>
                              <td class="width-15" >所属公司</td>
                              <td class="width-30"  >
                                  <el-form-item  prop="company.id">
                                  <SelectTree
                                              ref="company"
                                              :props="{
                                                  value: 'id',             // ID字段名
                                                  label: 'name',         // 显示名称
                                                  children: 'children'    // 子级字段名
                                                }"
                                              disabled
                                              url="/sys/office/treeData?type=2"
                                              :value="inputForm.company.id"
                                              :clearable="true"
                                              :accordion="true"
                                              @getValue="(value) => {inputForm.company.id=value}"/>
                                  </el-form-item>
                              </td>
                          </tr>
                          <tr  class="iconHidden">
                              <td class="width-15">申请时间</td>
                              <td class="width-30">
                                  <el-form-item prop="applydate" >
                                        <el-date-picker
                                              v-model="inputForm.applydate"
                                              type="datetime"
                                              style="width: 100%;"
                                              disabled
                                              value-format="yyyy-MM-dd HH:mm:ss"
                                              :clearable="false"
                                              placeholder="请选择">
                                            </el-date-picker>
                                  </el-form-item>
                              </td>
                              <td class="width-15">所属部门</td>
                              <td class="width-30">
                                  <el-form-item  prop="depart.id">
                                  <SelectTree
                                              ref="depart"
                                              :props="{
                                                  value: 'id',             // ID字段名
                                                  label: 'name',         // 显示名称
                                                  children: 'children'    // 子级字段名
                                                }"
                                              disabled
                                              url="/sys/office/treeData?type=2"
                                              :value="inputForm.depart.id"
                                              :clearable="true"
                                              :accordion="true"
                                              @getValue="(value) => {inputForm.depart.id=value}"/>
                                  </el-form-item>   
                              </td>
                          </tr>
                          <tr  class="iconHidden">
                              <td class="width-15"><span  style="color: red">*&nbsp;</span>地区</td>
                              <td class="width-30">
                                  <el-form-item prop="region"
                                        :rules="[{required: true, message:'地区不能为空', trigger:['blur','change']}]">
                                              <CityPicker
                                                style="width:100%"
                                                :value="inputForm.region"
                                                :clearable="true"                                            
                                                :accordion="true"
                                                @getValue="(value) => {inputForm.region=value}"/>
                                  </el-form-item>
                              </td>
                              <td class="width-15"></td>                             
                          </tr>
                          <tr style="line-height: 3.5;">
                              <td colspan="4"
                                  style="font-weight: bold;text-align: left;padding-left: 32px;position: relative;font-size: 14px">
                                  <img src="../../../../../assets/imgs/processManagement/basic.png" alt=""
                                      style="position: absolute;width: 20px;height: 20px;left: 0px;top: 14px;margin-left: 8px">
                                  福利信息
                              </td>
                          </tr>
                          <tr>
                              <td colspan="4" class="addDelImg">
                                  <i v-if="this.showButton" id="add" @click="add" class="el-icon-circle-plus"></i>
                                  <i v-if="this.showButton" id="sub" @click="delAll" class="el-icon-remove"></i>
                              </td>
                          </tr>
                          <tr>
                              <td colspan="4" class="elTable">
                                  <div style="width:100%">
                                      <el-table                 
                                        :data="tables"
                                        size="small" row-key="id" style="border-collapse:collapse;"                                          
                                        :cell-style="cellStyle_attached"  ref="mytables"
                                        :row-class-name="tableRowClassName"
                                        :header-cell-class-name="cellHeadStyle_attached"                                                      
                                        :tree-props="{ children: 'children', hasChildren: 'hasChildren'}"                  
                                        >
                                      <el-table-column type="selection" width="50" align="center"></el-table-column>
                                      <el-table-column prop="projectname" header-align="center" align="center" show-overflow-tooltip label="项目名称">
                                        <template slot="header">
                                            <span style="color: red">*&nbsp;</span>
                                            <span>项目名称</span>                                    
                                        </template>
                                        <template slot-scope="scope">
                                            <el-input v-model="scope.row.projectname"></el-input>
                                        </template>
                                      </el-table-column>
                                      <el-table-column prop="applymoney"   header-align="center" align="center" show-overflow-tooltip label="申请金额">
                                        <template slot="header">
                                            <span style="color: red">*&nbsp;</span>
                                            <span>申请金额</span>                                    
                                        </template>
                                        <template slot-scope="scope">
                                            <el-input v-model="scope.row.applymoney"></el-input>
                                        </template>
                                      </el-table-column>
                                      <el-table-column prop="explaind"  header-align="center"  align="center"  show-overflow-tooltip label="说明">
                                        <template slot="header">
                                            <span style="color: red">*&nbsp;</span>
                                            <span>说明</span>                                    
                                        </template>
                                        <template slot-scope="scope">
                                            <el-input v-model="scope.row.explaind"></el-input>
                                        </template>
                                      </el-table-column>
                                      <!-- <el-table-column prop="remarks"  header-align="center"  align="center"  show-overflow-tooltip  label="备注信息">
                                        <template slot-scope="scope">
                                            <el-input v-model="scope.row.remarks"></el-input>
                                        </template>
                                      </el-table-column> -->
                                      <!-- <el-table-column  label="操作" width="200">
                                        <template slot-scope="scope">
                                          <el-button @click="viewBenefitWelarRow(scope.row)" type="text" size="mini" :disabled="scope.row.projectname==''">查看</el-button>
                                          <el-button  @click="editBenefitWelarRow(scope.row)" type="text" size="mini" :disabled="scope.row.projectname==''">编辑</el-button>
                                          <el-button  @click="delBenefitWelarRow(scope.row)" type="text" size="mini" :disabled="scope.row.projectname==''">删除</el-button>
                                        </template>
                                      </el-table-column> -->
                                      </el-table>
                                  </div>
                              </td>
                          </tr>
                          <tr style="height: 80px;line-height: 80px">
                              <td class="width-15" style="line-height: 80px">备注
                              </td>
                              <td colspan="4" style="padding:0 10px">
                                <el-form-item  prop="remarks">
                                    <el-input v-model="inputForm.remarks"></el-input>
                                </el-form-item>
                              </td>
                          </tr>
                      </table>
                  </el-form>                
              </div>
          </div>
        </div>
      </div>
    </div>
    <BenefitWelarForm ref="benefitWelarForm" @addRow="saveBenefitWelarRow(arguments)"></BenefitWelarForm>
  </div>
</template>
<script>
  import BenefitWelarForm from './BenefitWelarForm'
  import UserSelect from '@/components/userSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import CityPicker from '@/components/cityPicker'
  import tableStyles from '../../../../../utils/mixins.js'
import { isError } from 'xe-utils'
  export default {
    mixins: [tableStyles],
    data () {
      return {
        title: '',
        method: '',
        loading: false,
        tables:[
           {
            projectname:'',
            applymoney:'',
            explaind:'',
            // remarks:'',
           } 
          ],
        inputForm: {
          id: '',
          benefitWelarList: [],
          applyuser: {
            id: ''
          },
          company: {
            id: ''
          },
          applydate: '',
          depart: {
            id: ''
          },
          region: '',
          remarks: ''
        },
        showButton:true,//按钮显示与否
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
      UserSelect,
      SelectTree,
      CityPicker,
      BenefitWelarForm
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields();
              if(this.$store.state.user.id!=''){
                var users=this.$store.state.user
              }else{
                var users=JSON.parse(sessionStorage.getItem("userInfo"));
              }
              this.inputForm.applyuser.id=users.id;
              this.inputForm.company.id=users.company.id;
              this.inputForm.depart.id=users.office.id;
              this.inputForm.applydate=this.getNowTime();
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    created(){
      window.addEventListener("beforeunload",()=>{
        sessionStorage.setItem("userInfo",JSON.stringify(this.$store.state.user))
      })
    },
    methods: {          
      //添加
      add(){
        let i=this.tables.length
        if( (i==0)||(this.tables[i-1].projectname!='' && this.tables[i-1].applymoney!='' && this.tables[i-1].explaind!='') ){
          let addTable={
            projectname:'',
            applymoney:'',
            explaind:'',
            // remarks:'',
          }
          this.tables.push(addTable);
        }else{
          this.$message.warning('请先填写当前已有行所有必填数据');
        } 
      },
      //添加行序号
      tableRowClassName({row, rowIndex}) {
        row.index = rowIndex;
      },
      //删除
      delAll() {
          // var items = document.getElementsByClassName("el-checkbox__original");
          // for (var j = 1; j < items.length; j++) {
          //     if (items[j].checked) //如果item被选中
          //     {
          //         var baba = items[j].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
          //         var erzi = items[j].parentNode.parentNode.parentNode.parentNode.parentNode
          //         baba.removeChild(erzi);
          //         j--;
          //     }
          // }
          for (var i = this.$refs.mytables.selection.length - 1; i >= 0; i--) {
            this.tables.splice(this.$refs.mytables.selection[i].index, 1)
          }
          this.$refs.mytables.clearSelection();
      },
      init (id) {
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            this.$http({
              url: `/dlyrl/oa/applybenefits/oaApplyBenefits/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.oaApplyBenefits);
              this.tables=data.oaApplyBenefits.benefitWelarList
              this.loading = false;
              if(this.$route.query.readOnly=='true'){
                this.showButton=false;
              } 
            })
          })
        }
      },
      saveBenefitWelarRow (child) {
        if (child[0] === '') {
          this.inputForm.benefitWelarList.push(child[1])
        } else {
          this.inputForm.benefitWelarList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.benefitWelarList.splice(index, 1, child[1])
            }
          })
        }
      },
      addBenefitWelarRow (child) {
        this.$refs.benefitWelarForm.init('add')
      },
      viewBenefitWelarRow (child) {
        this.$refs.benefitWelarForm.init('view', child)
      },
      editBenefitWelarRow (child) {
        this.$refs.benefitWelarForm.init('edit', child)
      },
      delBenefitWelarRow (child) {
        this.inputForm.benefitWelarList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.benefitWelarList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.benefitWelarList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      saveForm (callback) {
        for(let i=0;i<this.tables.length;i++){
          if(!this.tables[i].projectname){
            this.$message.warning('请选择第'+Number(i+1)+'条信息的项目名称');
              return false;
          }
          if(!this.tables[i].applymoney){
            this.$message.warning('请选择第'+Number(i+1)+'条信息的申请金额');
              return false;
          }
          if(!this.tables[i].explaind){
            this.$message.warning('请填写第'+Number(i+1)+'条信息的说明');
              return false;          
          }  
        }
        this.inputForm.benefitWelarList=this.tables;
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/oa/applybenefits/oaApplyBenefits/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                callback(data.businessTable, data.businessId);
                this.loading = false;
                this.$refs.inputForm.resetFields();
                this.inputForm.applyuser.id=this.$store.state.user.id;
                this.inputForm.company.id=this.$store.state.user.company.id;
                this.inputForm.depart.id=this.$store.state.user.office.id;
                this.inputForm.applydate= this.getNowTime();
              }
            })
          }
        })
      }
    }
  }
</script>
